<?php
namespace app\common\library;

use think\Exception;

class httpCurl
{
    public static function postData($content, $header, $url)
    {

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL,$url);  //设置url
        curl_setopt($curl, CURLOPT_HTTPHEADER,$header);
        curl_setopt($curl, CURLOPT_MAXREDIRS,10);
        curl_setopt($curl, CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在 设为0表示不检查证书 设为1表示检查证书中是否有CN(common name)字段 设为2表示在1的基础上校验当前的域名是否与CN匹配
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS,$content);
        curl_setopt($curl, CURLOPT_TIMEOUT, 0); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
        $response = curl_exec($curl);
        curl_close($curl);
        return  $response;
    }
    public static function get($url,$header, $param = array())
    {
        if (!is_array($param)) {
            throw new Exception("参数必须为array");
        }
        $p = '';
        foreach ($param as $key => $value) {
            $p = $p . $key . '=' . $value . '&';
        }
        if (preg_match('/\?[\d\D]+/', $url)) {//matched ?c
            $p = '&' . $p;
        } else if (preg_match('/\?$/', $url)) {//matched ?$
            $p = $p;
        } else {
            $p = '?' . $p;
        }
        $p = preg_replace('/&$/', '', $p);
        $url = $url . $p;
        //echo $url;
        $httph = curl_init($url);
        curl_setopt($httph, CURLOPT_SSL_VERIFYPEER, 0);
//        curl_setopt($httph, CURLOPT_SSL_VERIFYHOST, 1);
        curl_setopt($httph, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($httph, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
        curl_setopt($httph, CURLOPT_HTTPHEADER,$header);
//        curl_setopt($httph, CURLOPT_RETURNTRANSFER, 1);
//        curl_setopt($httph, CURLOPT_HEADER, 1);
        $rst = curl_exec($httph);
        curl_close($httph);
        return $rst;
    }
}